import {unref} from 'vue';
import type {Router, RouteLocationNormalized} from 'vue-router';
import nProgress from 'nprogress';

import {permissionGuard} from './permissionGuard';

export function setupRouterGuard(router: Router) {
  progressGuard(router);
  permissionGuard(router);
}

export function progressGuard(router: Router) {
  // const {getOpenNProgress} = useTransitionSetting();
  router.beforeEach(async (to) => {
    if (to.meta.loaded) {
      return true;
    }
    // unref(getOpenNProgress) && nProgress.start();
    console.log('createProgressGuard');
    nProgress.start();
    return true;
  });

  router.afterEach(async () => {
    // unref(getOpenNProgress) && nProgress.done();
    nProgress.done();
    return true;
  });
}
